<?php
use  Channel\Server;
use  Workerman\Worker;
require_once __DIR__ . '/../../vendor/autoload.php';

// 不传参数默认是监听0.0.0.0:2206
// 初始化一个 Channel 服务端
$channel_server = new Server('0.0.0.0', 2206);

// websocket 服务端
$worker = new Worker('websocket://0.0.0.0:4236');
$worker->name = 'websocket';
$worker->count = 6;
//每个 worker 进程启动时
$worker->onWorkerStart = function ($worker){
  // Channel 客户端连接到 Channel 服务端
    Channel\Client::connect('127.0.0.1',2206);
    // 订阅broadcast事件，并注册事件回调
    Channel\Client::on('broadcast',function ($event_data)use($worker){
        // 向当前worker进程的所有客户端广播消息
        foreach ($worker->connections as $connection){
            $connection->send($event_data);
        }
    });
};



$worker->onMessage = function($connection, $data)
{
    // 将客户端发来的数据当做事件数据
    $event_data = $data;
    // 向所有worker进程发布broadcast事件
    \Channel\Client::publish('broadcast', $event_data);
};




Worker::runAll();

//接收消息的连接
// 127.0.0.1换成实际workerman所在ip
//ws = new WebSocket("ws://127.0.0.1:4236");
//ws.onmessage = function(e) {
//    alert("收到服务端的消息：" + e.data);
//};
//广播消息
//ws.send('hello world');